package com.xunfang;

import java.util.*;

/**
 * @author yellow Doctor
 * @date 2025-09-09
 * @desc https://blog.csdn.net/hekai7217
 */
public class MapTest {

    public static void main(String[] args) {

        //定义一个map
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "aa");
        map.put("2", "bb");
        map.put("3", "cc");
        map.put("4", "dd");

        //hashmap的遍历:

        //1、拿到 EntrySet
        System.out.println("=============entrySet======================");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        // entry 就是一个k-v
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println("=============keySet======================");

        //2、可以拿到 keySet
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + ":" + map.get(key));
        }

        System.out.println("=============map.values======================");
        //这个可以拿到所有的值
        Collection<String> values = map.values();

//        for (String value : values) {
//            System.out.println(value);
//        }

        //迭代器
        Iterator<String> iterator = values.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //java8
        map.forEach((k, v) -> System.out.println(k + ":" + v));

    }

}
